package com.yitiao.data_algorithms.leetcode.editor.cn;

import java.util.HashMap;
import java.util.Map;

class MostFrequentEvenElement {
    // 2024-03-10 18:50:32
    public static void main(String[] args) {
        Solution solution = new MostFrequentEvenElement().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int mostFrequentEven(int[] nums) {
            Map<Integer, Integer> map = new HashMap<>();
            int max = 0;
            int num = -1;
            for (int j : nums) {
                if (j % 2 == 0) {
                    int cnt = map.getOrDefault(j, 0) + 1;
                    map.put(j, cnt);
                    if (cnt > max || cnt == max && j < num) {
                        max = cnt;
                        num = j;
                    }
                }
            }
            return num;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}